/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdQuests;

import com.SySammy.sdQuests.QuestMaker.QuestMethods;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

class sdQuestsCmds implements CommandExecutor {

    private sdQuests plugin;
    private QuestMethods QM = new QuestMethods();

    public sdQuestsCmds(sdQuests aThis) {
        this.plugin = aThis;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("quest")) {
            return QuestMethod(cs, Nargs);
        }
        return true;
    }

    private boolean QuestMethod(CommandSender cs, String[] args) {
        Player ply = (Player) cs;
        if (args.length <= 0) {
            try {
                if (!plugin.casUseQuestsList(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                QM.QuestList(ply);
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "Input error.");
            }
        } else if (args[0].equalsIgnoreCase("-i") && args.length >= 2) {
            try {
                if (!plugin.casUseQuestsInfo(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (!QM.QuestExists(ply, args[1].toLowerCase())) {
                    ply.sendMessage(ChatColor.RED + "That quest doesn't exists");
                    return true;
                }
                QM.QuestInfo(ply, args[1].toLowerCase());
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "Input error.");
            }
        } else if (args[0].equalsIgnoreCase("-s") && args.length >= 2) {
            try {
                if (!plugin.casUseQuestsStart(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }

                if (!QM.QuestExists(ply, args[1].toLowerCase())) {
                    ply.sendMessage(ChatColor.RED + "That quest doesn't exists");
                    return true;
                }
                QM.AcceptQuest(ply, args[1].toLowerCase());
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "Input error.");
            }

        } else if (args[0].equalsIgnoreCase("-r") && args.length >= 2) {
            try {
                if (!plugin.casUseQuestsEnd(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (!QM.QuestExists(ply, args[1].toLowerCase())) {
                    ply.sendMessage(ChatColor.RED + "That quest doesn't exists");
                    return true;
                }
                QM.ReturnQuest(ply, args[1].toLowerCase());
                return true;
            } catch (Exception e) {
                ply.sendMessage(ChatColor.RED + "Input error.");
            }
        } else if (args[0].equalsIgnoreCase("-h")) {
            if (plugin.casUseQuestsHelp(ply)) {
                try {
                    return false;
                } catch (Exception e) {
                    ply.sendMessage(ChatColor.RED + "Input error.");
                }
            } else {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
            }
        }
        return true;
    }
}
